package ch.zbw.sysventory.agent.socketserver.handlers.impl;

import java.io.DataOutputStream;

import ch.zbw.sysventory.agent.socketserver.handlers.AbstractClientRequestHandler;
import ch.zbw.sysventory.agent.utils.AgentPropertiesUtil;

public class NewComputerIdRequestHandler extends AbstractClientRequestHandler {

	private AgentPropertiesUtil agentPropertiesUtil;

	public NewComputerIdRequestHandler(AgentPropertiesUtil agentPropertiesUtil) {
		this.agentPropertiesUtil = agentPropertiesUtil;
	}

	@Override
	public void handleClientRequest(String requestMessage, DataOutputStream output) throws Exception {
		if (requestMessage.indexOf(":") > 0) {
			String newIdString = requestMessage.substring(requestMessage.indexOf(":") + 1);
			Long newId = Long.valueOf(newIdString);
			if (newId > 0) {
				this.agentPropertiesUtil.getProperties().put("sysventory.agent.computerid", newId + "");
				this.agentPropertiesUtil.save();
				this.sendResponseMessage(requestMessage, output, "NewIdIsSet");
				return;
			}
		}
		this.sendResponseMessage(requestMessage, output, "NewIdNotSet");
	}

}
